package it.antonioduca.citt;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CittService extends Service {

	private final String TAG = "Citt";

	private boolean enabled = false;
	private TelephonyManager telephony;
	private AudioManager audioManager;
	private SensorManager sensorManager;

	@Override
    public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		enabled = true;

		telephony = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
		audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
		sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
		
		telephony.listen(new CallListener(audioManager, sensorManager), PhoneStateListener.LISTEN_CALL_STATE);
		
		Log.i(TAG, "CittService started");
	}
	
    @Override
    public void onDestroy() {
        enabled = false;
        super.onDestroy();
    }
	
	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	private final ICittService.Stub mBinder = new ICittService.Stub() {
		@Override
		public int getPid() {
        	return Process.myPid();
        }

		@Override
		public boolean isEnabled() throws RemoteException {
			return enabled;
		}
    };
}
